// 接口请求前缀
let baseUrl = "";

//
if (process.env.NODE_ENV === "development") {
  baseUrl = "http://localhost:9898";
} else {
  baseUrl = "https://www.xxx.com";
}

export default function http(url: string, data = {}, method: string = "GET") {
  return new Promise((reslove, reject) => {
    uni.request({
      url: baseUrl + url,
      data,
      method,
      header: {
        token: uni.getStorageInfoSync("token") || "",
      },
      success: (res) => {
        // http的状态码判断
        if (res.statusCode === 200) {
          //接口业务状态码
          if (res.data.code === 1) {
            reslove(res?.data?.data);
          } else if (res.data.code === 0) {
            const { message: errorMsg = "" } = res.data;
            uni.showToast({
              title: errorMsg,
              icon: "none",
            });
            reject(errorMsg);
          }
        }
        // todo 401,403,404
      },
      fail: (err) => {
        uni.showToast({
          title: `请求异常${err}`,
          icon: "none",
        });
      },
    });
  });
}
